<template>
  <ContentWrap >
    <!-- 搜索工作栏 -->
    <span>设备：</span><span class="text-blue-500 cursor-pointer">{{ device?.name }}</span>
    <el-button
      class="ml-5"
      type="primary"
      plain
      @click="openForm('create')"
    >
      <Icon icon="ep:plus" class="mr-5px" />录入</el-button>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="设备编码" align="center" prop="code" />
      <el-table-column label="所属人" align="center" prop="ownerName" />
      <el-table-column label="单元" align="center" prop="unitName" />
      <el-table-column label="部门" align="center" prop="departmentName" />
      <el-table-column label="标签" align="center" prop="tag" />
      <el-table-column
        label="采购时间"
        align="center"
        prop="procurementTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="投入时间"
        align="center"
        prop="usageTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <DeviceInstanceForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { DeviceInstanceApi, DeviceInstanceVO } from '@/api/zjyj/deviceinstance'
import DeviceInstanceForm from './DeviceInstanceForm.vue'
import { DeviceApi } from '@/api/zjyj/device'

/** 设备实例 列表 */
defineOptions({ name: 'DeviceInstance' })
const props = defineProps<{
  deviceId: number // 流程实例的编号
}>()


const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<DeviceInstanceVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  createTime: [],
  deviceId: undefined,
  code: undefined,
  procurementTime: [],
  ownerId: undefined,
  tag: undefined,
  usageTime: [],
  unitId: undefined,
  departmentId: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await DeviceInstanceApi.getDeviceInstancePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await DeviceInstanceApi.deleteDeviceInstance(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await DeviceInstanceApi.exportDeviceInstance(queryParams)
    download.excel(data, '设备实例.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

const device= ref({
})
const getDeviceInfo = async () => {
  console.log(props.deviceId)
  device.value = await DeviceApi.getDevice(props.deviceId)
}

/** 初始化 **/
onMounted(async () => {
  console.log(props.deviceId)
  await getList()
  await getDeviceInfo()
})
</script>
